package com.scalesoft.artsync.frontend.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;

import com.scalesoft.artsync.frontend.ProgressDialog;
import com.scalesoft.artsync.frontend.SettingsDialog;
import com.scalesoft.artsync.frontend.renderer.PluginListCellRenderer;
import com.scalesoft.artsync.frontend.view.FrontendView;
import com.scalesoft.artsync.model.CONTENTLEVEL;
import com.scalesoft.artsync.model.submission.Submission;
import com.scalesoft.artsync.modelimpl.submission.SubmissionCollection;
import com.scalesoft.artsync.modelimpl.submission.artwork.ArtworkSubmissionImpl;
import com.scalesoft.artsync.plugins.AccountSettings;
import com.scalesoft.artsync.plugins.BasicAccountSettings;
import com.scalesoft.artsync.plugins.Exporter;
import com.scalesoft.artsync.plugins.Importer;
import com.scalesoft.artsync.plugins.Plugin;
import com.scalesoft.artsync.plugins.PluginRegistry;
import com.scalesoft.artsync.plugins.util.Log;
import com.scalesoft.artsync.util.Notifyable;

public class FrontendControllerImpl implements FrontendController, ActionListener, Notifyable {

	private FrontendView view;

	public FrontendControllerImpl(FrontendView view) {
		this.view = view;

		DefaultComboBoxModel importerComboModel = new DefaultComboBoxModel();
		for (Importer importer : PluginRegistry.getImporters()) {
			importerComboModel.addElement(importer);
		}
		PluginListCellRenderer importerComboRenderer = new PluginListCellRenderer();
		this.view.getComboImportPlugin().setRenderer(importerComboRenderer);
		this.view.getComboImportPlugin().setModel(importerComboModel);

		DefaultComboBoxModel exporterComboModel = new DefaultComboBoxModel();
		for (Exporter exporter : PluginRegistry.getExporters()) {
			exporterComboModel.addElement(exporter);
		}
		PluginListCellRenderer exporterComboRenderer = new PluginListCellRenderer();
		this.view.getComboExportPlugin().setRenderer(exporterComboRenderer);
		this.view.getComboExportPlugin().setModel(exporterComboModel);
		this.view.getComboExportPlugin().setSelectedIndex(PluginRegistry.getExporters().size() - 1);

		this.view.getButtonDoImport().addActionListener(this);
		this.view.getButtonDoExport().addActionListener(this);
		this.view.getButtonImportPluginSettings().addActionListener(this);
		this.view.getButtonExportPluginSettings().addActionListener(this);
		this.view.getButtonInvertSelection().addActionListener(this);
		this.view.getButtonSelectAll().addActionListener(this);
		this.view.getButtonSelectNone().addActionListener(this);
		this.view.getButtonNewSubmission().addActionListener(this);

		SubmissionCollection.loadSubmissionData();
		this.view.getSubmissionTableModel().setData(SubmissionCollection.getInstance().getSubmissions());


	}

	// -----------------------------------------------------------------
	// Actions
	// -----------------------------------------------------------------

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == view.getButtonDoImport()) {
			Importer importer = (Importer) this.view.getComboImportPlugin().getSelectedItem();
			importer.setProgressHandler(ProgressDialog.getInstance());
			importer.setNotifyable(this);
			try {
				Thread importerThread = new Thread(importer);
				importerThread.start();
				this.view.getSubmissionTableModel().setData(SubmissionCollection.getInstance().getSubmissions());
			} catch (Exception e) {
				Log.e(this, "Error in actionPerformed()", e);
			}
		}
		else if (event.getSource() == view.getButtonDoExport()) {
			Exporter exporter = (Exporter) this.view.getComboExportPlugin().getSelectedItem();
			exporter.setProgressHandler(ProgressDialog.getInstance());
			try {
				Thread exporterThread = new Thread(exporter);
				exporterThread.start();
			} catch (Exception e) {
				Log.e(this, "Error in actionPerformed()", e);
			}
		}
		else if (event.getSource() == view.getButtonImportPluginSettings()) {
			Plugin plugin = (Plugin) this.view.getComboImportPlugin().getSelectedItem();
			AccountSettings settings = plugin.getSettings();
			settings.loadSettings();
			SettingsDialog settingsDialog = new SettingsDialog(null, plugin.getPluginDisplayName(), (BasicAccountSettings) settings);
			settingsDialog.setVisible(true);
			settingsDialog.toFront();
		}
		else if (event.getSource() == view.getButtonExportPluginSettings()) {
			Plugin plugin = (Plugin) this.view.getComboExportPlugin().getSelectedItem();
			AccountSettings settings = plugin.getSettings();
			settings.loadSettings();
			SettingsDialog settingsDialog = new SettingsDialog(null, plugin.getPluginDisplayName(), (BasicAccountSettings) settings);
			settingsDialog.setVisible(true);
			settingsDialog.toFront();
		}
		else if (event.getSource() == view.getButtonNewSubmission()) {
//TODO build a submission editing form
//			Submission s = newSubmissionController.newSubmissionDialog();
//			Submission s = new ArtworkSubmissionImpl();
//			s.setTitle("test");
//			s.setDescription("durrrrrr");
//			s.setContentLevel(CONTENTLEVEL.CLEAN);
//			s.setSelected(new Boolean(true));
//			s.setTags("test tag, another test tag");
//			ArrayList<Submission> data = this.view.getSubmissionTableModel().getData();
//			data.add(s);
//			this.view.getSubmissionTableModel().setData(data);
		}
		else if (event.getSource() == view.getButtonSelectNone()) {
			ArrayList<Submission> data = SubmissionCollection.getInstance().getSubmissions();
			for (Submission s : data) {
				s.setSelected(false);
			}
			this.view.getSubmissionTableModel().setData(data);
		}
		else if (event.getSource() == view.getButtonSelectAll()) {
			ArrayList<Submission> data = SubmissionCollection.getInstance().getSubmissions();
			for (Submission s : data) {
				s.setSelected(true);
			}
			this.view.getSubmissionTableModel().setData(data);
		}
		else if (event.getSource() == view.getButtonInvertSelection()) {
			ArrayList<Submission> data = SubmissionCollection.getInstance().getSubmissions();
			for (Submission s : data) {
				s.setSelected(!s.isSelected());
			}
			this.view.getSubmissionTableModel().setData(data);
		}

	}

	@Override
	public void notifyListener() {
		ArrayList<Submission> data = SubmissionCollection.getInstance().getSubmissions();
		this.view.getSubmissionTableModel().setData(data);
	}

	// -----------------------------------------------------------------
	// Getters/Setters
	// -----------------------------------------------------------------

	public FrontendView getView() {
		return view;
	}

	public void setView(FrontendView view) {
		this.view = view;
	}

}
